/**
 * Filename: ShareUtils.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/17 22:11
 */
package com.github.postsanf.yinian.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.github.postsanf.yinian.R;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;

import static com.tencent.mm.opensdk.modelmsg.SendMessageToWX.Req.WXSceneSession;

/**
 * 分享接口
 */
public class ShareUtils {
    //*****************************图片分享*********************************

    /**
     * 分享给朋友
     */
    public static void shareToSession() {

    }

    /**
     * 分享到朋友圈
     */
    public static void shareToTimeline() {

    }

    /**
     * 分享到QQ
     */
    public static void shareToQQ() {

    }

    /**
     * 分享到空间
     */
    public static void shareToQQzone() {

    }

    /**
     * 分享到微博
     */
    public static void shareToWeibo() {

    }


    //*****************************邀请*********************************

    /**
     * 邀请微信好友
     */
    public static void onInvateFromWx(IWXAPI api, Context context, String Url, String title, String description) {
        // 初始化WXWebObject 对象
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = Url;                                    // 邀请页面地址
        // 初始化WXMediaMessage对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;                                // 网页标题
        msg.description = description;                            // 网页描述
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.send_wx);
        msg.setThumbImage(thumb);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;            // 分享给好友   SendMessageToWX.Req.WXSceneTimeline（朋友圈）
        // 调用api接口发送数据到微信
        api.sendReq(req);
    }

    /**
     * 分享到朋友圈
     */
    public static void onShareToTimeLine(IWXAPI api, Context context, String Url, String title, String description) {
        // 初始化WXWebObject 对象
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = Url;                                    // 邀请页面地址
        // 初始化WXMediaMessage对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;                                // 网页标题
        msg.description = description;                            // 网页描述
        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.send_wx);
        msg.setThumbImage(thumb);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;            // 分享朋友圈
        // 调用api接口发送数据到微信
        api.sendReq(req);
    }

    public static void sendMiniApps(IWXAPI api, Context context, String title, String content,
                                    String url, int groupid) {
        WXMiniProgramObject miniProgram = new WXMiniProgramObject();
        miniProgram.webpageUrl = url;
        //跳转的小程序的原始 ID
        miniProgram.userName = "gh_11ddcc4aabbb";
        //小程序的 Path
        miniProgram.path = "pages/viewscoll/viewscoll?groupid=" + groupid + "&from=grouplist";
        WXMediaMessage msg = new WXMediaMessage(miniProgram);
        msg.title = title;
        msg.description = content;
        Bitmap temp = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_launcher);
        msg.setThumbImage(temp);
        //使用此方法会出现无法分享的问题
//        Bitmap thumbBmp = Bitmap.createScaledBitmap(icon, 150, 150, true);
//        icon.recycle();
//        msg.thumbData = BitmapUtils.bitmapToByteArray(thumbBmp, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = WXSceneSession;
        api.sendReq(req);
    }

    /**
     * 邀请QQ好友
     */
    public static void onInvateFromQQ(Tencent mTencent, Activity activity, String Url, String title, String description, IUiListener listener) {
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);            // 限30个字符
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, description);        // 限40个字符
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, Url);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://7kttjt.com1.z0.glb.clouddn.com/image/view/app_icons/645682d422a97c3f097350048f748c70/120");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "忆年");
        mTencent.shareToQQ(activity, params, listener);
    }
}
